package com.tuanzi.loan.web.bean.wallet;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.wallet.LoanUserWallet;
import com.tuanzi.loan.business.entity.wallet.LoanUserWalletFlow;
import com.tuanzi.loan.business.service.wallet.LoanUserWalletFlowService;
import com.tuanzi.loan.business.service.wallet.LoanUserWalletService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.exception.ValidateException;
import com.tuanzi.loan.vo.request.wallet.LoanUserWalletRequest;
import com.tuanzi.loan.vo.response.wallet.LoanUserWalletResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.wallet.LoanUserWalletLazyDataModel;

/**
 * 用户钱包管理后台Bean
 * @author chenweijun 2018/02/16
 *
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanUserWalletListBean extends JSFListingBean<LoanUserWalletRequest, LoanUserWalletResponse> {
	 
	 
	private static final long serialVersionUID = 3469366783016301393L;
	
	@Autowired
	private LoanUserWalletService loanUserWalletService;
	
	@Autowired
	private LoanUserWalletFlowService loanUserWalletFlowService;
	
	private LoanUserWallet loanUserWallet;
	
	@PostConstruct
	private void init() {
		initLazyDataModel();
	}
	
	private void initLazyDataModel() {
		$ = new LoanUserWalletRequest();
		lazyDataModel = new LoanUserWalletLazyDataModel($);
	}
	
	/**
	 * 查看钱包流水
	 * @return
	 */
	public String onViewLoanUserWalletFlow() {
        return redirect("/views/wallet/loan-user-wallet-flow.xhtml");
    }
	
	/**
	 * 选中用户钱包
	 * @param id
	 */
	public void onSelect(Long id){
		loanUserWallet = loanUserWalletService.findById(id);
	}
	
	/**
	 * 修改用户钱包余额
	 * @param id
	 */
	public void onEdit(Long id){
		Float amount = $.getAmount();
		String remark = $.getRemark();
		
		if(amount == null){
			throw new ValidateException("请输入修改金额");
		}  
		
		if(StringUtils.isBlank(remark)){
			throw new ValidateException("请输入备注");
		}
		
		//1.修改钱包余额
		LoanUserWallet wallet = loanUserWalletService.findById(id);
		Float balance = wallet.getBalance() + amount;
		Long userId = wallet.getUserId();
		wallet.setBalance(balance);
		wallet.setUpdateTime(new Date());
		wallet.setUpdator(CurrentUser.get().getName());
		loanUserWalletService.saveOrUpdate(wallet);
		
		//2.增加钱包操作流水
		LoanUserWalletFlow flow = new LoanUserWalletFlow();
		flow.setUserId(userId);
		flow.setAmount(amount);
		flow.setBalance(balance);
		flow.setRemark(remark);
		flow.setCreateTime(new Date());
		flow.setCreator(CurrentUser.get().getName());
		
		loanUserWalletFlowService.saveOrUpdate(flow);
		
		$.setAmount(null);
		$.setRemark(null);
		
	}
	
}
